

console.log("get add cart");
setTimeout(() => {
    getTypeIdNeed();
}, 2000);


function getTypeIdNeed(){

   /* $.ajax({
        url: "https://www.ozonzhuli.com/index.php?c=api/neiyi/get_add_cart_type_id",
        success: function (data) {
            if(data.data.type_id_List){
                console.log(data.data.type_id_List);
                getAddCart(data.data.type_id_List, 1,0);
            }
        }
    });*/

    getAddCart(1,0);

}

function getAddCart(page,index){

    console.log("index:"+index +" page:"+page);
    // 创建一个空对象用于保存请求数据
    var dataRequest = {};

    // 获取当前日期
    var today = new Date();

    // 获取七天前的日期
    var sevenDaysAgo = new Date();
    sevenDaysAgo.setDate(today.getDate() - 30); // 设置为七天前


// 设置 date_from 和 date_to
    dataRequest.date_from = formatDate(sevenDaysAgo) + "T00:00:00"; // 七天前的日期的零点
    dataRequest.date_to = formatDate(today) + "T23:59:59"; // 今天的日期的最后一刻

    console.log(dataRequest);

    // 设置其他属性
    dataRequest.page_size = "100";
    dataRequest.page = page;
    dataRequest.sort_direction = "Desc"

    const  baseUrl = "https://seller.ozon.ru/api/site/self-gateway/api/accruals/details";
    const params = new URLSearchParams(dataRequest);
    const fullUrl = `${baseUrl}?${params.toString()}`;

    // 将 dataRequest 对象转换为 JSON 字符串并输出
    var jsonString = JSON.stringify(dataRequest);
    console.log(jsonString);
     $.ajax({
        url: fullUrl,
　　　　 type: 'GET',
        processData: false,
        contentType: 'application/json',
        headers: {
            'X-O3-App-Name': 'seller-ui',
            'X-O3-Company-Id': '1274960',
            'X-O3-Language': 'zh-Hans',
            'X-O3-Page-Type': 'products-other'
          },
        xhrFields: {
            withCredentials: true // 这里设置了withCredentials
        },
        success: function(data) {
             console.log(data);
             if (data.items) {
                 var id_list = [];
                 data.items.forEach(item => {
                     id_list.push(item['id']);
                 });
                 $.post("https://www.ozonzhuli.com/index.php?c=api/neiyi/post_add_money",
                      {data:JSON.stringify(data.items)},
                      function(data){
                        //console.log(data);
                      },
                  "text");

                  if(id_list.length==100){
                      setTimeout(() => {
                          page = page + 1;
                          getAddCart(page,index)
                      }, 5000);
                  }else{
                      console.log("sleep one day")
                      setTimeout(() => {
                          location.reload();
                      }, 3600000);
                  }
             }
        },
        error: function(err) {
            console.error(err);
        }
    });
}

function formatDate(date) {
    var year = date.getFullYear();
    var month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，+1
    var day = String(date.getDate()).padStart(2, '0');

    return `${year}-${month}-${day}`;
}


